//
//  XTBDNetWork.m
//  xingtongbeidou
//
//  Created by 赵国成 on 15/2/26.
//  Copyright (c) 2015年 mac mini01. All rights reserved.
//

#import "XTBDNetWork.h"
#import "JSONKit.h"
#import "AFNetworking.h"
#import "XTBDShareDataClass.h"
@implementation Result

@end

@implementation XTBDNetWork

+(void)PostUrl:(NSString *)url Parameter:(NSMutableDictionary *)dictionary andData:(void (^)(Result *))block
{
    
     Result *result = [[Result alloc]init];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager] ;
    manager.responseSerializer = [AFJSONResponseSerializer serializer];//使用这个将得到的是JSON

     manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    NSString * postUrl = [NSString stringWithFormat:@"%@%@",POST_JLG_URL,url];
    [manager POST:postUrl parameters:dictionary success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"发送Url:%@",url);
//        NSLog(@"发送参数:%@",dictionary.JSONString);
   //    NSLog(@"接收数据:%@",operation.responseString);
//        NSLog(@"接收数据类型:%@",NSStringFromClass([operation.responseString.objectFromJSONString[@"data"] class]));
        id objcet = operation.responseString.objectFromJSONString;
        if (objcet[@"Error"]) {
            
            if ([NSStringFromClass([objcet[@"data"] class])isEqualToString:@"AG_SKJDictionary"])
            {
                result.dataDic = objcet[@"data"];
            }
            else if([NSStringFromClass([objcet[@"data"] class])isEqualToString:@"JKString"])
            {
                result.dataStr = objcet[@"data"];
            }else
            {
                result.dataArr = objcet[@"data"];
            }
            result.status = XTBDRequestDataOK;
        }else if([objcet[@"status"] isEqualToString:@"15"])
        {
            result.msg = [XTBDNetWork errorMsg:[objcet[@"status"]intValue]];
            result.status = XTBDRequestDataPermission;
        }else
        {
            result.msg = [XTBDNetWork errorMsg:[objcet[@"status"]intValue]];
            result.status = XTBDRequestDataError;
        }
        block(result);        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error:%@",error);
        NSLog(@"ErrorOperation:%@",operation.responseString);
        NSLog(@"发送Url:%@",url);

        result.status = XTBDRequestDataTimeOut;
        result.msg = @"登录失效请重新登录";
        block(result);
    }];
}
+(void)PostVideoUrl:(NSString*)url Parameter:(NSMutableDictionary*)dictionary andData:(void(^)(Result *result))block{
    Result *result = [[Result alloc]init];
    AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
    
     NSString * postUrl = [NSString stringWithFormat:@"%@%@",POST_JLG_URL,url];
    
    // 2. Create an `NSMutableURLRequest`.
    NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:postUrl parameters:@{}
                                                    constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                                                        
    [formData appendPartWithFileURL:[NSURL URLWithString:[dictionary objectForKey:@"pathUrl"]] name:[dictionary objectForKey:@"name"] fileName:[dictionary objectForKey:@"name"] mimeType:@"video/mpeg" error:nil];
                                                        
                                                    }];
    
    // 3. Create and use `AFHTTPRequestOperationManager` to create an `AFHTTPRequestOperation` from the `NSMutableURLRequest` that we just created.
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
     manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    AFHTTPRequestOperation *operation =
    [manager HTTPRequestOperationWithRequest:request
                                     success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                         
                                         id objcet = operation.responseString.objectFromJSONString;
                                         NSLog(@"objectFromJSONString===%@",operation.responseString.objectFromJSONString);
                                         
                                         result.dataArr = objcet;
                                         block(result);
                                         
                                         NSLog(@"Success %@", responseObject);
                                     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                         NSLog(@"Failure====== %@", error.description);
                                         result.status = XTBDRequestDataTimeOut;
                                         result.msg = @"登录失效请重新登录";
                                         block(result);
                                         
//                                         NSLog(@"bbbbbbbbbbb");
                                         
                                     }];
    
    // 4. Set the progress block of the operation.
    [operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten,
                                        long long totalBytesWritten,
                                        long long totalBytesExpectedToWrite) {
        //        NSLog(@"Wrote %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite);
        
        
        Result *result = [[Result alloc]init];
        
        result.doubleStr = (float)totalBytesWritten/totalBytesExpectedToWrite;
        NSLog(@"=====%f",(float)totalBytesWritten/totalBytesExpectedToWrite);
        
        block(result);
        
        
        
    }];
    
    // 5. Begin!
    [operation start];
    
    
}
+(void)PostImageUrl:(NSString*)url Parameter:(NSMutableDictionary*)dictionary andData:(void(^)(Result *result))block{

    getUserID;
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat =@"yyyyMMddHHmmss";
    NSString *str = [formatter stringFromDate:[NSDate date]];
    NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
    NSDictionary *  dic = @{@"m_token":userAppToken};

    
    Result *result = [[Result alloc]init];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager] ;
    manager.responseSerializer = [AFJSONResponseSerializer serializer];//使用这个将得到的是JSON
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    NSString * postUrl = [NSString stringWithFormat:@"%@%@",POST_JLG_URL,url];
    
    NSLog(@"postUrl=====%@",postUrl);
    [manager POST:postUrl parameters:dic constructingBodyWithBlock:^(id <AFMultipartFormData> _Nonnull formData){
   
        //上传的参数(上传图片，以文件流的格式)
        [formData appendPartWithFileData:[dictionary objectForKey:@"name"]
                                    name:@"file"
                                fileName:fileName
                                mimeType:@"image/jpeg"];    
        
    
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"发送Url:%@",url);
             NSLog(@"发送参数:%@",dictionary.JSONString);
             NSLog(@"接收数据:%@",operation.responseString);
        
        id objcet = operation.responseString.objectFromJSONString;
        result.dataArr = objcet;
        block(result);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error:%@",error);
        NSLog(@"ErrorOperation:%@",operation.responseString);
        NSLog(@"发送Url:%@",url);
        
        result.status = XTBDRequestDataTimeOut;
        result.msg = @"登录失效请重新登录";
        block(result);
    }];
    

    
    

}

+(void)PostOrderAudioeUrl:(NSString*)url Parameter:(NSMutableDictionary*)dictionary andData:(void(^)(Result *result))block{
    
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat =@"yyyyMMddHHmmss";
    NSString *str = [formatter stringFromDate:[NSDate date]];
    NSString *fileName = [NSString stringWithFormat:@"%@.mp3", str];
    
    NSLog(@"%@",dictionary);
    
    Result *result = [[Result alloc]init];
    AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
    
    NSString * postUrl = [NSString stringWithFormat:@"%@%@",POST_JLG_URL,url];
    
    // 2. Create an `NSMutableURLRequest`.
    NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:postUrl parameters:@{@"tel":[dictionary objectForKey:@"tel"],@"uid":[dictionary objectForKey:@"uid"],@"duration":[dictionary objectForKey:@"duration"],@"city":[dictionary objectForKey:@"city"]}
    constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                                                        
    [formData appendPartWithFileData:[dictionary objectForKey:@"name"] name:@"voice-ServerSend.mp3" fileName:fileName mimeType:@"application/octet-stream"];
//    [formData appendPartWithFormData:[dictionary objectForKey:@"name"] name:fileName];
                                                    }];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    AFHTTPRequestOperation *operation =
    [manager HTTPRequestOperationWithRequest:request
                                     success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                         
                                         id objcet = operation.responseString.objectFromJSONString;
                                         NSLog(@"objectFromJSONString===%@",operation.responseString.objectFromJSONString);
                                         
                                         result.dataArr = objcet;
                                         block(result);
                                         
                                         NSLog(@"Success %@", responseObject);
                                     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                         NSLog(@"Failure====== %@", error.description);
                                         result.status = XTBDRequestDataTimeOut;
                                         result.msg = @"登录失效请重新登录";
                                         block(result);
                                         
                                         //                                         NSLog(@"bbbbbbbbbbb");
                                         
                                     }];
    
    // 4. Set the progress block of the operation.
    [operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten,
                                        long long totalBytesWritten,
                                        long long totalBytesExpectedToWrite) {
        //        NSLog(@"Wrote %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite);
        
        
        Result *result = [[Result alloc]init];
        
        result.doubleStr = (float)totalBytesWritten/totalBytesExpectedToWrite;
        NSLog(@"=====%f",(float)totalBytesWritten/totalBytesExpectedToWrite);
        
        block(result);
        
        
        
    }];
    
    // 5. Begin!
    [operation start];
    
    
    
    
}


+(void)PostAudioeUrl:(NSString*)url Parameter:(NSMutableDictionary*)dictionary andData:(void(^)(Result *result))block{
    
   
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        formatter.dateFormat =@"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.mp3", str];
        
        //上传的参数(上传图片，以文件流的格式)
//        NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithDictionary:@{@"pathUrl":[NSString stringWithFormat:@"%@",mp3FilePath],@"name":@"voice-ServerSend"}];

     //        [formData appendPartWithFileData:[dictionary objectForKey:@"name"]
//                                    name:@"file"
//                                fileName:fileName
//                                mimeType:@"application/octet-stream"];
        
    
    Result *result = [[Result alloc]init];
    AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
    
    NSString * postUrl = [NSString stringWithFormat:@"%@%@",POST_JLG_URL,url];
    
    // 2. Create an `NSMutableURLRequest`.
    NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:postUrl parameters:@{}
                                                    constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                                                        
//  [formData appendPartWithFileURL:[NSURL URLWithString:[dictionary objectForKey:@"pathUrl"]] name:[dictionary objectForKey:@"name"] fileName:fileName mimeType:@"application/octet-stream" error:nil];
    [formData appendPartWithFileData:[dictionary objectForKey:@"name"] name:@"voice-ServerSend.mp3" fileName:fileName mimeType:@"application/octet-stream"];
                                                        
                                                    }];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    AFHTTPRequestOperation *operation =
    [manager HTTPRequestOperationWithRequest:request
                                     success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                         
                                         id objcet = operation.responseString.objectFromJSONString;
                                         NSLog(@"objectFromJSONString===%@",operation.responseString.objectFromJSONString);
                                         
                                         result.dataArr = objcet;
                                         block(result);
                                         
                                         NSLog(@"Success %@", responseObject);
                                     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                         NSLog(@"Failure====== %@", error.description);
                                         result.status = XTBDRequestDataTimeOut;
                                         result.msg = @"登录失效请重新登录";
                                         block(result);
                                         
                                         //                                         NSLog(@"bbbbbbbbbbb");
                                         
                                     }];
    
    // 4. Set the progress block of the operation.
    [operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten,
                                        long long totalBytesWritten,
                                        long long totalBytesExpectedToWrite) {
        //        NSLog(@"Wrote %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite);
        
        
        Result *result = [[Result alloc]init];
        
        result.doubleStr = (float)totalBytesWritten/totalBytesExpectedToWrite;
        NSLog(@"=====%f",(float)totalBytesWritten/totalBytesExpectedToWrite);
        
        block(result);
        
        
        
    }];
    
    // 5. Begin!
    [operation start];
    

    
    
}

+(void)PostImageUrl:(NSString*)url Parameter:(NSMutableDictionary*)dictionary andDic:(NSDictionary*)userDic andData:(void(^)(Result *result))block{
    
    
    
    Result *result = [[Result alloc]init];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager] ;
    manager.responseSerializer = [AFJSONResponseSerializer serializer];//使用这个将得到的是JSON
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    NSString * postUrl = [NSString stringWithFormat:@"%@%@",POST_JLG_URL,url];
    
    NSLog(@"postUrl=====%@",postUrl);
    [manager POST:postUrl parameters:userDic constructingBodyWithBlock:^(id <AFMultipartFormData> _Nonnull formData){
        
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        formatter.dateFormat =@"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
        
        //上传的参数(上传图片，以文件流的格式)
        [formData appendPartWithFileData:[dictionary objectForKey:@"image"]
                                    name:@"image"
                                fileName:fileName
                                mimeType:@"image/jpeg"];
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"发送Url:%@",url);
        NSLog(@"发送参数:%@",dictionary.JSONString);
        NSLog(@"接收数据:%@",operation.responseString);
        
        id objcet = operation.responseString.objectFromJSONString;
        result.dataArr = objcet;
        block(result);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error:%@",error);
        NSLog(@"ErrorOperation:%@",operation.responseString);
        NSLog(@"发送Url:%@",url);
        
        result.status = XTBDRequestDataTimeOut;
        result.msg = @"登录失效请重新登录";
        block(result);
    }];
    
    
    
    
    
}

+(void)taglistUrl:(NSString *)url Parameter:(NSMutableDictionary *)dictionary andData:(void (^)(Result *))block
{
    Result *result = [[Result alloc]init];
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager] ;
    manager.responseSerializer = [AFJSONResponseSerializer serializer];//使用这个将得到的是JSON

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    NSString * postUrl = [NSString stringWithFormat:@"%@%@",POST_JLG_URL,url];
    [manager POST:postUrl parameters:dictionary success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"发送Url:%@",url);
//        if ([url isEqualToString:@"/Service/createService"]) {
//            NSLog(@"发送参数:%@",dictionary.JSONString);
//        }
     
//       NSLog(@"接收数据:%@",operation.responseString);
        
        id objcet = operation.responseString.objectFromJSONString;
        result.dataArr = objcet;
        block(result);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//         NSLog(@"发送参数:%@",dictionary.JSONString);
        NSLog(@"Error:%@",error);
        NSLog(@"ErrorOperation:%@",operation.responseString);
//        NSLog(@"发送Url:%@",url);
//        NSLog(@"发送参数:%@",dictionary.JSONString);
//        if ([url isEqualToString:@"/Service/createService"]) {
//            NSLog(@"发送参数:%@",dictionary.JSONString);
//        }

        
        result.status = XTBDRequestDataTimeOut;
        result.msg = @"登录失效请重新登录";
        block(result);
    }];
}


+(void)mapSeachUrl:(NSString *)url Parameter:(NSMutableDictionary *)dictionary andData:(void (^)(Result *))block
{
    Result *result = [[Result alloc]init];
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager] ;
    manager.responseSerializer = [AFJSONResponseSerializer serializer];//使用这个将得到的是JSON
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html",@"text/javascript", nil];
//    NSString * postUrl = [NSString stringWithFormat:@"%@%@",POST_JLG_URL,url];
    [manager GET:url parameters:dictionary success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"发送Url:%@",url);
        //        NSLog(@"发送参数:%@",dictionary.JSONString);
        //       NSLog(@"接收数据:%@",operation.responseString);
        
        id objcet = operation.responseString.objectFromJSONString;
        result.dataArr = objcet;
        block(result);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error:%@",error);
        //        NSLog(@"ErrorOperation:%@",operation.responseString);
        //        NSLog(@"发送Url:%@",url);
        //        NSLog(@"发送参数:%@",dictionary.JSONString);
        
        result.status = XTBDRequestDataTimeOut;
        result.msg = @"登录失效请重新登录";
        block(result);
    }];
}

+(void)weatherUrl:(NSString *)url Parameter:(NSMutableDictionary *)dictionary andData:(void (^)(Result *))block
{
    Result *result = [[Result alloc]init];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager] ;
    manager.responseSerializer = [AFJSONResponseSerializer serializer];//使用这个将得到的是JSON
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    NSString * postUrl = [NSString stringWithFormat:@"%@",url];
    [manager POST:postUrl parameters:dictionary success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"发送Url:%@",url);
        //        NSLog(@"发送参数:%@",dictionary.JSONString);
        NSLog(@"接收数据:%@",operation.responseString);
        
        id objcet = operation.responseString.objectFromJSONString;
        result.dataArr = objcet;
        block(result);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error:%@",error);
        NSLog(@"ErrorOperation:%@",operation.responseString);
        NSLog(@"发送Url:%@",url);
        //        NSLog(@"发送参数:%@",dictionary.JSONString);
        
        result.status = XTBDRequestDataTimeOut;
        result.msg = @"登录失效请重新登录";
        block(result);
    }];
}


+(void)zhinajin:(NSString*)url Parameter:(NSMutableDictionary*)dictionary andData:(void(^)(Result *result))block{

    Result *result = [[Result alloc]init];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager] ;
    manager.responseSerializer = [AFJSONResponseSerializer serializer];//使用这个将得到的是JSON
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];

    [manager POST:url parameters:dictionary success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"发送Url:%@",url);
        //        NSLog(@"发送参数:%@",dictionary.JSONString);
        NSLog(@"接收数据:%@",operation.responseString);
        
        id objcet = operation.responseString.objectFromJSONString;
        result.dataArr = objcet;
        block(result);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error:%@",error);
        //        NSLog(@"ErrorOperation:%@",operation.responseString);
        NSLog(@"发送Url:%@",url);
        //        NSLog(@"发送参数:%@",dictionary.JSONString);
        
        result.status = XTBDRequestDataTimeOut;
        result.msg = @"登录失效请重新登录";
        block(result);
    }];


}
+(NSString*)errorMsg:(NSInteger)status
{
    NSString *returnMsg = @"";
    switch (status) {
        case 0:
            returnMsg = @"请求成功";
            break;
        case 1:
            returnMsg = @"请求失败";
            break;
        case 2:
            returnMsg = @"查询无结果";// 分页
            break;
        case 3:
            returnMsg = @"请求的数据不存在";// 单条
            break;
        case 4:
            returnMsg = @"请登录";
            break;
        case 5:
            returnMsg = @"重复操作";
            break;
        case 6:
            returnMsg = @"用户名或昵称已存在";
            break;
        case 7:
            returnMsg = @"参数不正确";
            break;
        case 8:
            returnMsg = @"验证码出错";
            break;
        case 9:
            returnMsg = @"该账户已冻结,请联系星通客服";
            break;
        case 10:
            returnMsg = @"订单已完结";
            break;
        case 11:
            returnMsg = @"订单已存在";
            break;
        case 12:
            returnMsg = @"不可修改";
            break;
        case 13:
            returnMsg = @"已删除的数据";
            break;
        case 14:
            returnMsg = @"已存在的数据";
            break;
        case 15:
            returnMsg = @"无权限";
            break;
        case 16:
            returnMsg = @"恶意操作";
            break;
        case 17:
            returnMsg = @"订单未确认";
            break;
        case 18:
            returnMsg = @"订单未送货";
            break;
        case 19:
            returnMsg = @"退单中";
            break;
        case 20:
            returnMsg = @"退单中";
            break;
        case 21:
            returnMsg = @"不可删除的数据";
            break;
        case 22:
            returnMsg = @"用户名不存在";
            break;
        case 23:
            returnMsg = @"用户不存在";
            break;
        case 24:
            returnMsg = @"无效的应户名或密码";
            break;
        case 25:
            returnMsg = @"请求超时,请重新调试";
            break;
        default:
            returnMsg = @"操作失败";
            break;
    }
    return returnMsg;
}

+ (SecIdentityRef)identityWithCert{
    NSString *path = [[[NSBundle mainBundle]bundlePath]stringByAppendingPathComponent:@"xingtongResource.bundle/Contents/Resources/files/client.p12"];
    NSData *p12Data = [NSData dataWithContentsOfFile:path];
    
    SecIdentityRef identity = NULL;
    SecCertificateRef certificate = NULL;
    
    if (!p12Data) {
        NSLog(@"没找到p12文件");
        return nil;
    }
    
    [[self class] identyty:&identity andCertificate:&certificate fromPKCS12Data:p12Data];
    return identity;
}

+(BOOL) identyty:(SecIdentityRef*)outIdentity andCertificate:(SecCertificateRef*)outCert fromPKCS12Data:(NSData*)inPKCS12Data{
    
    OSStatus securityError = errSecSuccess;
    
    NSDictionary *optionsDictionary = [NSDictionary dictionaryWithObject:@"bluemobi" forKey:(__bridge id)kSecImportExportPassphrase];
    
    CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
    securityError = SecPKCS12Import((__bridge CFDataRef)inPKCS12Data, (__bridge CFDictionaryRef)optionsDictionary, &items);
    
    if (securityError == 0) {
        CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex(items, 0);
        const void *tempIdentity = NULL;
        tempIdentity = CFDictionaryGetValue(myIdentityAndTrust, kSecImportItemIdentity);
        *outIdentity = (SecIdentityRef)tempIdentity;
        const void *tempCert = NULL;
        tempCert = CFDictionaryGetValue(myIdentityAndTrust, kSecImportItemCertChain);
        *outCert = (SecCertificateRef)tempCert;
    }
    else{
        //        NSLog(@"SSSLLL ERROR %d", (int)securityError);
        return NO;
    }
    
    return YES;
}

//身份证
+(BOOL)IdCodeNum:(NSString *)value
{
    value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([value length] != 18) {
        return NO;
    }
    NSString *mmdd = @"(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8])))";
    NSString *leapMmdd = @"0229";
    NSString *year = @"(19|20)[0-9]{2}";
    NSString *leapYear = @"(19|20)(0[48]|[2468][048]|[13579][26])";
    NSString *yearMmdd = [NSString stringWithFormat:@"%@%@", year, mmdd];
    NSString *leapyearMmdd = [NSString stringWithFormat:@"%@%@", leapYear, leapMmdd];
    NSString *yyyyMmdd = [NSString stringWithFormat:@"((%@)|(%@)|(%@))", yearMmdd, leapyearMmdd, @"20000229"];
    NSString *area = @"(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|82|[7-9]1)[0-9]{4}";
    NSString *regex = [NSString stringWithFormat:@"%@%@%@", area, yyyyMmdd  , @"[0-9]{3}[0-9Xx]"];
    
    NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    if (![regexTest evaluateWithObject:value]) {
        return NO;
    }
    int summary = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7
    + ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9
    + ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10
    + ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5
    + ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8
    + ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4
    + ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2
    + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6
    + [value substringWithRange:NSMakeRange(9,1)].intValue *3;
    NSInteger remainder = summary % 11;
    NSString *checkBit = @"";
    NSString *checkString = @"10X98765432";
    checkBit = [checkString substringWithRange:NSMakeRange(remainder,1)];// 判断校验位
    return [checkBit isEqualToString:[[value substringWithRange:NSMakeRange(17,1)] uppercaseString]];
}

//验证电话
+(BOOL)checkInputAccount:(NSString *)text
{
    
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    
    NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    BOOL res1 = [regextestmobile evaluateWithObject:text];
    BOOL res2 = [regextestcm evaluateWithObject:text];
    BOOL res3 = [regextestcu evaluateWithObject:text];
    BOOL res4 = [regextestct evaluateWithObject:text];
    
    if (res1 || res2 || res3 || res4 )
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

+(BOOL)checkInputTelephone:(NSString *)text
{
    NSString *Regex = @"^[0-9]*$";
    
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    
    return[emailTest evaluateWithObject:text];
}

//检验密码
+(BOOL)checkInputPassword:(NSString *)str
{
    NSString *Regex = @"(.*\\W+.*\\d+.*)|(.*\\d+.*\\W+.*)|(.*\\d+.*[A-Za-z]+.*)|(.*[A-Za-z]+.*\\d+.*)|(.*[A-Za-z]+.*\\W+.*)|(.*\\W+.*[A-Za-z]+.*)";
    
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    
    return[emailTest evaluateWithObject:str];
}

@end
